package room

import (
	"errors"
	"server/common"

	"github.com/mitchellh/mapstructure"
	"github.com/sirupsen/logrus"
)

// -------------------------------------------------------------------------------------------------
//
// -------------------------------------------------------------------------------------------------
func (s *RoomService) handleConnectionClosedNotify(msg common.Message, from common.ServiceInfo,
	connId string) error {
	LOG().WithFields(logrus.Fields{
		"msg":    common.ToJson(msg),
		"from":   from,
		"connId": connId,
	}).Info("Handling connection closed notify")

	if msg.Data == nil {
		return errors.New("connection closed notify data is nil")
	}

	var data common.ConnClosedNotifyData
	if err := mapstructure.Decode(msg.Data, &data); err != nil {
		return errors.New("failed to decode connection closed notify data: " + err.Error())
	}

	s.mediaMgr.OnConnectionClosed(data.ConnId)

	return nil
}
